import { useDict } from "@/stores/dictStore"


export function Qs(params) {
  const queryString = Object.entries(params)
    .filter(([key, value]) => value !== "")
    .map(([key, value]) => `${key}=${value}`)
    .join("&");
  return queryString
}
export function isFirstCharChinese(str) {
  if (typeof str !== 'string' || str.length === 0) {
    return false; // 如果不是字符串或者字符串为空，返回false
  }
  const firstChar = str.charAt(0);
  return /[\u4e00-\u9fff]/.test(firstChar);
}


function handleOrgText(mainBusinessUnitOrgCode,row) {
  const dictStore = useDict()
  const { userLdList, orgList } = toRefs(dictStore)
  if (mainBusinessUnitOrgCode) {
    let arr = mainBusinessUnitOrgCode.split(',')
    let nameArr = []
    arr.forEach(item => {
      let target = orgList.value.find(org => org.code == item)
      if (target) {
        nameArr.push(target.name)
      }
    })
    return nameArr.join(',')
  } else {
    return row.businessUnit
  }

}
function handleOrgText2(businessUnitOrgCodes,row) {
  const dictStore = useDict()
  const { userLdList, orgList } = toRefs(dictStore)
  if (businessUnitOrgCodes) {
    let arr = businessUnitOrgCodes.split(',')
    let nameArr = []
    arr.forEach(item => {
      let target = orgList.value.find(org => org.code == item)
      if (target) {
        nameArr.push(target.name)
      }
    })
    return nameArr.join(',')
  } else {
    return row.businessUnit
  }
}
function handleLdText(text) {
  const dictStore = useDict()
  const { userLdList, orgList } = toRefs(dictStore)
  if (!text) {
    return ""
  }
  if (isFirstCharChinese(text)) {
    return text
  }
  let arr = text.split(',')
  let nameArr = []
  arr.forEach(item => {
    let name = userLdList.value.find(user => user.objectid == item)
    if (name) {
      nameArr.push(name.userName)
    }
  })
  return nameArr.join(',')
}

export function codeToText(row, field, type) {
  if (type == 'org') {
    return handleOrgText(row[field],row)
  }
  else if (type == 'org2') {
    return handleOrgText2(row[field],row)
  }
  else if (type == 'ld') {
    return handleLdText(row[field])
  }
}